﻿using CodeMap.DbModels;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CodeMap
{
    public class CodeRun
    {


        public void run(string Name, RunViewModel vm) 
        { 
            string template=File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory+Name+".code");
            string code = generateCode(template,vm);
            //生成到应用根目录



            var s = File.CreateText(AppDomain.CurrentDomain.BaseDirectory + Name + "Entity.cs");
            s.Write(code);
            s.Flush();
            s.Close();
            Console.WriteLine(code);
        }

        public string generateCode(string template, RunViewModel vm)
        {
            string columnTemplate = template.Split("{forColumn1}")[1].Split("{forColumn2}")[0];
            template = template.Replace(columnTemplate, "");
            StringBuilder columnStr = new StringBuilder();
            foreach (var c in vm.columns)
            {
                string colRs = columnTemplate
                    .Replace("{columnDesc}", c.columnDesc)
                    .Replace("{columnName}", c.columnName)
                    .Replace("{entityType}", c.entityType)
                    .Replace("{entityName}", c.entityName);
                columnStr.Append(colRs);
            }

            string rs = template.Replace("{moduleName}", vm.moduleName)
                .Replace("{tableName}", vm.tableName)
                .Replace("{nowDate}", vm.nowDate)
                .Replace("{Desc}", vm.Desc)
                .Replace("{forColumn1}", columnStr.ToString())
                .Replace("{forColumn2}", "");
            return rs;
        }



        public TableInfo GetTableInfo(string connectionString, string tableName)
        {
            var tableInfo = new TableInfo { TableName = tableName, Columns = new List<ColumnInfo>() };
            using (var conn = new MySqlConnection(connectionString))
            {
                conn.Open();
                // 获取表注释
                using (var cmd = new MySqlCommand(
                    "SELECT table_comment FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = @tableName", conn))
                {
                    cmd.Parameters.AddWithValue("@tableName", tableName);
                    using (var reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            tableInfo.TableDesc = reader["table_comment"].ToString();
                        }
                    }
                }
                // 获取字段信息
                using (var cmd = new MySqlCommand(
                    @"SELECT COLUMN_NAME, COLUMN_TYPE, COLUMN_COMMENT 
              FROM information_schema.columns 
              WHERE table_schema = DATABASE() AND table_name = @tableName", conn))
                {
                    cmd.Parameters.AddWithValue("@tableName", tableName);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            tableInfo.Columns.Add(new ColumnInfo
                            {
                                ColumnName = reader["COLUMN_NAME"].ToString(),
                                DataType = reader["COLUMN_TYPE"].ToString(),
                                ColumnDesc = reader["COLUMN_COMMENT"].ToString()
                            });
                        }
                    }
                }
            }
            return tableInfo;
        }




    }
}
